﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

/// <summary>
/// Web control customized for purposes of this web. 
/// That include custom:
/// * drop down lists (ObjectSelection)
/// * textboxes (ExtendedFormFIeld)
/// ScrumWebControl consist of two web controls: label on the left and web control on the right (e.g drop down list)
/// ScrumWebControl is represented as table row with three columns in HTML.
/// On left side of the control is always displayed it's label. Labels text is associated with LabelText property.
/// Each instance can be in two modes: readonly or read/write mode, which is ensured by Enabled property
/// Usage:
/// ScrumWebControls are used in ObjectDetails and can be also used in filters (drop down lists only).
/// </summary>
public abstract class ScrumWebControl : UserControl
{
    /// <summary>
    /// Text of scrum user control's Label, which is displayed on the left side of control.
    /// On ExtendedFormFields has to be set, otherwise control will not load.
    /// </summary>
    /// <value>The label.</value>
    public virtual string LabelText 
    { 
        get
        {
            return this.label.Text;
        }
        set
        {
            this.label.Text = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="ScrumWebControl"/> 
    /// can be manipulated by user or is displaying it's value only(readonly).
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    public virtual bool Enabled
    {
        get
        {
            return this.webControl.Enabled;
        }
        set
        {
            this.webControl.Enabled = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="ScrumWebControl"/> 
    /// is readonly.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    public virtual bool ReadOnly 
    {
        get
        {
            return this._readonly;
        }
        set
        {
            this._readonly = value;
        }
    }
    private bool _readonly = false;

    /// <summary>
    /// Gets or sets the width.
    /// </summary>
    /// <value>The width.</value>
    [DefaultValue(100)]
    public virtual Unit Width 
    {
        get
        {
            return this.webControl.Width;
        }
        set
        {
            this.webControl.Width = value;
        }
    }

    /// <summary>
    /// Renders the content of a scrum web control
    /// </summary>
    /// <param name="writer">Writer</param>
    protected override void Render(HtmlTextWriter writer)
    {
        if (String.IsNullOrEmpty(this.LabelText))
            throw new NullReferenceException(String.Format("LabelText property must be set on a ScrumUserControl with ID={0}",this.ID));
        base.Render(writer);
    }

    protected WebControl webControl
    {
        get
        {
            if (this.webControl_ == null) webControl_ = GetWebControl();
            return this.webControl_;
        }
    }
    protected abstract WebControl GetWebControl();
    private WebControl webControl_;

    private Label label 
    {
        get
        {
            if (label_ == null) label_ = GetLabel();
            return label_;
        }
        set
        {
            label_ = value;
        }
    }
    private Label label_;
    protected abstract Label GetLabel();

}
